package org.bigmarketapi.projectreview.DesingMode.PipeLineMode;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: ProjectReview
 * @BelongsPackage: org.bigmarketapi.projectreview.DesingMode.PipeLineMode
 * @Author: dujiaqi
 * @CreateTime: 2024-12-14  22:40
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class PipelineExecutor {
    public Map<Class<? extends PipelineContext>, List<? extends ContextHandler<? extends PipelineContext>>> handlerMap=new HashMap<>();

    public PipelineExecutor(Map<Class<? extends PipelineContext>, List<? extends ContextHandler<? extends PipelineContext>>> handlerMap) {
        this.handlerMap = handlerMap;
    }

    public boolean accept(PipelineContext context) {
        final Class<? extends PipelineContext> dataType = context.getClass();
        final List<? extends ContextHandler<? extends PipelineContext>> handlers = handlerMap.get(dataType);
        boolean lastSuccess = true;
        for (ContextHandler<? extends PipelineContext> handler : handlers) {
            lastSuccess = handler.handle(context);
            if (!lastSuccess) break;
        }
        return  lastSuccess;
    }
}
